//
//  HGPluginViewController.m
//  HGMMT
//
//  Created by huan liu on 2018/12/26.
//  Copyright © 2018年 eport_user1. All rights reserved.
//

#import "HGPluginViewController.h"
#import "LinkViewController.h"
#import "LYYNavigationController.h"




@interface HGPluginViewController ()

@property(nonatomic,assign)BOOL isConnected; //是否还是在连接状态,默认为yes

@end

@implementation HGPluginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isConnected = YES;
    
    [self initNav];
    
    [self _initBottomButton];
    [self _initGoBackButton];
    // Do any additional setup after loading the view.
    
    //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notAction:) name:Link_Disconnect object:nil];
    
    
}

-(void)initNav{
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:nil];
    self.navigationItem.backBarButtonItem  = item;
    
}


-(void)startPageSetting:(PluginModel *)model{
    
  //  self.startPage = model.root;
    NSString *host = [NSString stringWithFormat:@"%@/index.html",model.root];
    self.startPage = host;
}


-(void)_initBottomButton{
    
    CGFloat dis = isIphoneX ? 34 : 0;
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(10, kScreenHeight - 50 - dis, 60, 40);
    button.backgroundColor = HGBTN_BackgoundColor;
    [button setTitle:@"退出" forState:(UIControlStateNormal)];
    [button setTitleColor:HGBTN_TitleColor forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
    
    
}

-(void)_initGoBackButton{
    
     CGFloat dis = isIphoneX ? 34 : 0;
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(kScreenWidth - 70, kScreenHeight - 50 - dis, 60, 40);
    button.backgroundColor = HGBTN_BackgoundColor;
    [button setTitle:@"返回" forState:(UIControlStateNormal)];
    [button setTitleColor:HGBTN_TitleColor forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(goBackButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
    
    
}

-(void)goBackButtonAction:(UIButton *)btn{

    if (self.isConnected)
    {
        UIWebView *web = (UIWebView *)self.webView;
        
        [web canGoBack] ? [web goBack] : [self.navigationController popToRootViewControllerAnimated:YES];
        
        
    }
    else
    {
        
        UIAlertController *alerVC =[UIAlertController alertControllerWithTitle:@"提示" message:@"连接已经断开,请重新进行连接" preferredStyle:(UIAlertControllerStyleAlert)];
        [alerVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
            LinkViewController *link = [[LinkViewController alloc]init];
            LYYNavigationController *nav = [[LYYNavigationController alloc]initWithRootViewController:link];
            keyWindow.rootViewController = nav;
            
        }]];
        
        [self presentViewController:alerVC animated:YES completion:nil];
        
        
    }
    

    
    

}



-(void)buttonAction:(UIButton *)btn{
    
    if (self.isConnected)
    {
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else
    {
        
            UIAlertController *alerVC =[UIAlertController alertControllerWithTitle:@"提示" message:@"连接已经断开,请重新进行连接" preferredStyle:(UIAlertControllerStyleAlert)];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
                UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
                LinkViewController *link = [[LinkViewController alloc]init];
                LYYNavigationController *nav = [[LYYNavigationController alloc]initWithRootViewController:link];
                keyWindow.rootViewController = nav;
        
            }]];
        
            [self presentViewController:alerVC animated:YES completion:nil];
        
        
    }
    
    
    
}


#pragma mark - 通知

-(void)notAction:(NSNotification *)not{
    
    
    self.isConnected = NO;
    

}

-(void)dealloc{
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



#pragma mark - 视图控制器的一些方法


-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    // self.navigationController.navigationBar.hidden == YES;
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
   
    self.navigationController.navigationBar.hidden = NO;
    // 开启返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}




@end



@implementation HGPluginCommandDelegate

/* To override the methods, uncomment the line in the init function(s)
 in MainViewController.m
 */

#pragma mark CDVCommandDelegate implementation

- (id)getCommandInstance:(NSString*)className
{
    return [super getCommandInstance:className];
}

/*
 NOTE: this will only inspect execute calls coming explicitly from native plugins,
 not the commandQueue (from JavaScript). To see execute calls from JavaScript, see
 MainCommandQueue below
 */


- (NSString*)pathForResource:(NSString*)resourcepath;
{
    return [super pathForResource:resourcepath];
}

@end

@implementation HGPluginCommandQueue

/* To override, uncomment the line in the init function(s)
 in MainViewController.m
 */
- (BOOL)execute:(CDVInvokedUrlCommand*)command
{
    return [super execute:command];
}


@end
